﻿using System.Collections;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.Specialized;
using System.Runtime.CompilerServices;

namespace HobSoft.SoccerBet2014.Common.Wpf.Data
{
    public class PagingCollectionView : ListCollectionView
    {
        //private readonly IList _innerList;
        private readonly int _itemsPerPage;

        private int _currentPage = 1;

        public PagingCollectionView(IList innerList, int itemsPerPage)
            : base(innerList)
        {
            
            //this._innerList = innerList;
            this._itemsPerPage = itemsPerPage;
            ((INotifyCollectionChanged)SortDescriptions).CollectionChanged += SortDescriptions_CollectionChanged;
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
        {
            if (args.Action == NotifyCollectionChangedAction.Add || args.Action == NotifyCollectionChangedAction.Remove)
            {
                OnPropertyChanged(new PropertyChangedEventArgs("PageCount"));
            }
            base.OnCollectionChanged(args);
        }

        private void SortDescriptions_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.Refresh();
        }

        public override int Count
        {
            get
            {
                if (base.Count == 0) return 0;
                var count = base.Count - (_currentPage - 1) * _itemsPerPage;
                return count > _itemsPerPage ? _itemsPerPage : count;
            }
        }

        public int CurrentPage
        {
            get { return this._currentPage; }
            set
            {
                if (this._currentPage == value) return;
                this._currentPage = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentPage"));
                this.Refresh();
            }
        }

        public int ItemsPerPage { get { return this._itemsPerPage; } }

        public int PageCount
        {
            get
            {
                return (base.Count + this._itemsPerPage - 1)
                    / this._itemsPerPage;
            }
        }

        private int EndIndex
        {
            get
            {
                var end = this._currentPage * this._itemsPerPage - 1;
                return (end > base.Count) ? base.Count : end;
            }
        }

        private int StartIndex
        {
            get { return (this._currentPage - 1) * this._itemsPerPage; }
        }

        public override object GetItemAt(int index)
        {
            if (base.Count == 0) return base.GetItemAt(index);
            var offset = index % (this._itemsPerPage);
            //return this._innerList[this.StartIndex + offset];
            return base.GetItemAt(this.StartIndex + offset);
        }

        public void MoveToNextPage()
        {
            if (this._currentPage < this.PageCount)
            {
                this.CurrentPage += 1;
            }
            this.Refresh();
        }

        public void MoveToPreviousPage()
        {
            if (this._currentPage > 1)
            {
                this.CurrentPage -= 1;
            }
            this.Refresh();
        }
    }
}
